﻿define(['admin/app', 'service/admin/bazaarOrderService'], function (app) {
    "use strict";
    app.controller('bazaarOrderController', function ($scope, $state, $stateParams, $location, utils, w5cValidator, bazaarOrderService, baseService) {
        $scope.list = {};
        $scope.query = { page: 1, size: 20};
        $.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                bazaarOrderService.gets($scope.query).success(function (response) { $scope.model = response.data; });
            },
            search_page: function (page) {
                $scope.query.page = page;
                $location.search('page', $scope.query.page);
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    bazaarOrderService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                
                } 
            },
            enable: function (model, isEnable) {
                var modal = utils.confirm({ msg: isEnable ? "您确定要禁用课程么" : "您确定要启用课程么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    bazaarOrderService.enable(model, isEnable).success(function () {
                        methods.search();
                    });
                });
            },
            cancle: function (id) {
                var modal = utils.confirm({ msg: "确定要取消交易么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    $scope.psdisabled = true;
                    bazaarOrderService.cancle(id).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        $scope.psdisabled = false;
                        if (response.result_code == 1000) {
                            methods.search();
                        }
                    }).error(function () { $scope.psdisabled = false; });
                });
            },
            receive: function (id) {
                var modal = utils.confirm({ msg: "确定要收货么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    $scope.psdisabled = true;
                    bazaarOrderService.receive(id).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        $scope.psdisabled = false;
                        if (response.result_code == 1000) {
                            methods.search();
                        }
                    }).error(function () { $scope.psdisabled = false; });
                });
            },
            save: function () {
                var modal = utils.confirm({ msg: "确定要保存么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    var model = $scope.model;
                    $scope.psdisabled = true;
                    bazaarOrderService.save(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        $scope.psdisabled = false;
                        if (response.result_code == 1000) {
                            methods.goBack();
                        }
                    }).error(function () { $scope.psdisabled = false; });
                });
            },
            goBack: function () {
                history.back();
            }
        };

        angular.extend($scope, methods);
        if ($state.includes('app.bazaarOrder.detail')) {
            methods.edit();
        } else {
            methods.search();
        }
    });
});